package com.atguigu.flink.chapter06;

import com.atguigu.flink.bean.UserBehavior;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

import java.util.HashSet;


public class Flink02_Project_UV {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(2);

        env
                .readTextFile("input/UserBehavior.csv")
                .flatMap(new FlatMapFunction<String, UserBehavior>() {
                    @Override
                    public void flatMap(String line, Collector<UserBehavior> out) throws Exception {
                        String[] data = line.split(",");
                        UserBehavior ub = new UserBehavior(
                                Long.valueOf(data[0]),
                                Long.valueOf(data[1]),
                                Integer.valueOf(data[2]),
                                data[3],
                                Long.valueOf(data[4])
                        );
                        if ("pv".equals(ub.getBehavior())) {
                            out.collect(ub);
                        }
                    }
                })
                .keyBy(UserBehavior::getBehavior)
                .process(new KeyedProcessFunction<String, UserBehavior, Long>() {
                    HashSet<Long> userIds = new HashSet<>();

                    @Override
                    public void processElement(UserBehavior value, KeyedProcessFunction<String, UserBehavior, Long>.Context ctx, Collector<Long> out) throws Exception {
/*                        // 计算有多少个独立的userId
                        int pre = userIds.size();
                        userIds.add(value.getUserId());
                        int current = userIds.size();

                        if (current > pre) {

                            out.collect((long) userIds.size());
                        }*/

                        if (userIds.add(value.getUserId())) {
                            out.collect((long) userIds.size());
                        }
                    }
                })
                .print();

        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
